//
//  BasicDownloadImgView.m
//  audi-services
//
//  Created by 安泽旭 on 13-11-1.
//  Copyright (c) 2013年 zhangkongli. All rights reserved.
//

#import "BasicDownloadImgView.h"
#import "BaseViewController.h"
#import "TMCache.h"

@implementation BasicDownloadImgView

- (void)setImageURL:(NSString *)url
{
    _imageURL = url;
    
    __block BasicDownloadImgView *blockSelf = self;
    if ([[TMDiskCache sharedCache] objectForKey:url] == nil) {
        [self setImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]]
                    placeholderImage:[UIImage imageNamed:_placeholderImageName]
                             success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                 [blockSelf setImageOnMainThread:image];
                                 [[TMDiskCache sharedCache] setObject:image forKey:url];
                             }
                             failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                 NSLog(@"BasicDownloadImgView Error -----> : %@",error);
                                 
                                 [blockSelf setImageOnMainThread:nil];
                             }];
    } else {
        [self setImageOnMainThread:(UIImage *)[[TMDiskCache sharedCache] objectForKey:url]];
    }
}

- (void)setImageOnMainThread:(UIImage *)image
{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (!image)
            return;
        self.image = image;
    });
}

@end
